package cc.vv.lkimagecomponent2.controllerplugin;

import android.view.View;

import cc.vv.lkimagecomponent2.lib.LoadFactory;

/**
 * @author dupeng dupeng@vv.cc
 * @date 2018/5/3
 */

public class ControlPlugin {
    private static ControlPlugin instance;

    public static ControlPlugin getInstance() {
        if (instance == null) {
            synchronized (ControlPlugin.class) {
                if (instance == null) {
                    instance = new ControlPlugin();
                }
            }
        }
        return instance;
    }

    /**
     * 开始所有图片请求
     */
    public void resumeRequests() {
        LoadFactory.getInstance().getEngine().resumeRequests();
    }

    /**
     * 暂停所有图片请求
     */
    public void pauseRequests() {
        LoadFactory.getInstance().getEngine().pauseRequests();
    }

    /**
     * 清空view中的图片缓存
     *
     * @param view 要清空的view
     */
    public void clearMemoryCache(View view) {
        LoadFactory.getInstance().getEngine().clearMemoryCache(view);
    }

    /**
     * 清空运存中的图片缓存
     */
    public void clearMemory() {
        LoadFactory.getInstance().getEngine().clearMemory();
    }

    /**
     * 清空所有缓存.内存,sd.
     * 注意：回调是子线程！
     */
    public void clearAllMemoryCaches(final ClearCallback callback) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                LoadFactory.getInstance().getEngine().clearAllMemoryCaches();
                if (callback != null) {
                    callback.onClearSuccess();
                }
            }
        }.start();
    }

    /**
     * 清空外存图片缓存
     * 注意：回调是子线程！
     */
    public void clearDiskCache(final ClearCallback callback) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                LoadFactory.getInstance().getEngine().clearDiskCache();
                if (callback != null) {
                    callback.onClearSuccess();
                }
            }
        }.start();
    }

    public interface ClearCallback {
        void onClearSuccess();
    }

}
